package com.lmoumou.lib_calendarview.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 * @author Lmoumou
 * @date : 2019/11/6 18:36
 */
class MenstruationStartView : View {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int = 0) : super(context, attrs, defStyle)


    private val mPaint by lazy {
        Paint().apply {
            color = Color.parseColor("#FE7E6D")
            strokeWidth = 3F
            isAntiAlias = true
            pathEffect = DashPathEffect(floatArrayOf(20F, 10F), 0F)
            style = Paint.Style.STROKE
        }
    }


    private val mRect by lazy { Rect() }

    private var type = 3


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val with = View.MeasureSpec.getSize(widthMeasureSpec)
        val height = View.MeasureSpec.getSize(heightMeasureSpec)
        mRect.set(0, 0, with, height)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.let {
            when (type) {
                1 -> {
                    drawTypeOne(it)
                }
                2 -> {
                    drawTypeTwo(it)
                }
                3 -> {
                    drawTypeThree(it)
                }
            }
        }
    }

    private fun drawTypeOne(canvas: Canvas) {
        val left = mRect.left + mPaint.strokeWidth
        val top = mRect.top + mPaint.strokeWidth
        val right = left + mRect.height() - mPaint.strokeWidth
        val bottom = mRect.bottom - mPaint.strokeWidth
        canvas.drawArc(left, top, right, bottom, 90F, 180F, false, mPaint)
        canvas.drawLine(
            left + mRect.height() / 2 - mPaint.strokeWidth,
            top,
            mRect.right - mPaint.strokeWidth,
            top,
            mPaint
        )
        canvas.drawLine(
            left + mRect.height() / 2 - mPaint.strokeWidth,
            bottom,
            mRect.right - mPaint.strokeWidth,
            bottom,
            mPaint
        )
    }

    private fun drawTypeTwo(canvas: Canvas) {
        val left = mRect.left + mPaint.strokeWidth
        val top = mRect.top + mPaint.strokeWidth
        val right = mRect.right - mPaint.strokeWidth
        val bottom = mRect.bottom - mPaint.strokeWidth
        canvas.drawLine(left, top, right, top, mPaint)
        canvas.drawLine(left, bottom, right, bottom, mPaint)
    }

    private fun drawTypeThree(canvas: Canvas) {
        val right = mRect.right - mPaint.strokeWidth
        val left = right - mRect.height() + mPaint.strokeWidth
        val top = mRect.top + mPaint.strokeWidth
        val bottom = mRect.bottom - mPaint.strokeWidth
        canvas.drawArc(left, top, right, bottom, 270F, 180F, false, mPaint)
        canvas.drawLine(mRect.left + mPaint.strokeWidth, top, left + mRect.height() / 2, top, mPaint)
        canvas.drawLine(mRect.left + mPaint.strokeWidth, bottom, left + mRect.height() / 2, bottom, mPaint)
    }

    fun setType(type:Int){
        this.type=type
        invalidate()
    }
}